package ru.urfc.portal.model;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

import ru.urfc.portal.model.accounting.Contragent;
import ru.urfc.portal.ui.client.Constants;

@Table(uniqueConstraints = @UniqueConstraint(columnNames = {"contragentId", "branch_id"}))
@Entity
@XmlRootElement(name = Constants.CLIENT)
public class Client extends SysObject {
	@Column(nullable = false)
	private Long contragentId;
	@Transient
	private Contragent contragent;
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(nullable = false)
	private Branch branch;
	@Column(nullable = false)
	private boolean accessEnabled;
	@Column(nullable = false)
	private boolean adminRegistered;
	@JoinColumn(nullable = false)
	@OneToOne(cascade = {CascadeType.PERSIST})
	private NotificationSettings notificationSettings;
	
	public Client() {
	}

	public Client(Long id, Long contragentId, Branch branch, boolean accessEnabled, boolean adminRegistered) {
		super(id);
		this.contragentId = contragentId;
		this.branch = branch; 
		this.accessEnabled = accessEnabled;
		this.adminRegistered = adminRegistered;
	}

	public Client(Contragent contragent, Branch branch, NotificationSettings notificationSettings) {
		this.contragentId = contragent.getId();
		this.branch = branch;
		this.notificationSettings = notificationSettings;
	}

	public void update(ClientUpdate clientUpdate) {
		this.accessEnabled = clientUpdate.getAccessEnabled();
	}

	@XmlTransient
	public Long getContragentId() {
		return contragentId;
	}

	public void setContragentId(Long contragentId) {
		this.contragentId = contragentId;
	}

	@XmlElement
	public Branch getBranch() {
		return branch;
	}

	public boolean isAccessEnabled() {
		return accessEnabled;
	}

	public void setAccessEnabled(boolean accessEnabled) {
		this.accessEnabled = accessEnabled;
	}

	public boolean isAdminRegistered() {
		return adminRegistered;
	}

	public void setAdminRegistered(boolean adminRegistered) {
		this.adminRegistered = adminRegistered;
	}

	public Contragent getContragent() {
		return contragent;
	}

	public void setContragent(Contragent contragent) {
		this.contragent = contragent;
	}
	
	@XmlTransient
	public NotificationSettings getNotificationSettings() {
		return notificationSettings;
	}
	
	public void setNotificationSettings(NotificationSettings notificationSettings) {
		this.notificationSettings = notificationSettings;
	}
	
}
